﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace UtilReportOMS
{
    /// <summary>
    /// Interaction logic for Uc_Trinhky.xaml
    /// </summary>
    public partial class Uc_Trinhky : UserControl
    {
        private enum STATUS { ON_POPUP, ON_WINDOW };
        private STATUS _status = STATUS.ON_POPUP;

        Popup ParentPopup;//= new Popup(); // popup
        UIElement ParentPage;
        Window ParentWindow;

#region Construction For Popup

        public Uc_Trinhky(Popup _pop,UIElement sender)
        {
            ParentPopup = _pop;
            _status = STATUS.ON_POPUP;
            ParentPage = sender;
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Uc_Trinhky_Loaded);
            _pop.Closed += new EventHandler(_pop_Closed);                   
        }

        void _pop_Closed(object sender, EventArgs e)
        {
            ParentPage.IsEnabled = true;
        }

#endregion

        public Uc_Trinhky(Window w, UIElement sender)
        {
            ParentWindow = w;
            ParentPage = sender;
            _status = STATUS.ON_WINDOW;
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Uc_Trinhky_Loaded);
            w.Closed += new EventHandler(w_Closed);      
        }

        void w_Closed(object sender, EventArgs e)
        {
        
            ParentPage.IsEnabled = true;

        }

        void Uc_Trinhky_Loaded(object sender, RoutedEventArgs e)
        {
            this.PreviewKeyDown += new KeyEventHandler(Uc_Trinhky_PreviewKeyDown);
            if (_status == STATUS.ON_POPUP)
            {
                //Width = 845;// System.Windows.SystemParameters.PrimaryScreenWidth;            
                //Height = 545;//System.Windows.SystemParameters.PrimaryScreenHeight;
                if (ParentPopup != null)
                {
                    ParentPopup.Height = 550;
                    ParentPopup.Width = 750;
                  //  ParentPopup.UpdateLayout();
                }              
            }                
            else { // chưa tính
            }
        }

        void Uc_Trinhky_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                uc_Exit(this);
            }
        }

#region My method trình ký

        //Set Info to Uc Trình ký.
        public void setinfor(CrystalDecisions.CrystalReports.Engine.ReportDocument rpttam, string dvqly, string[] FieldsNames, string[] Formulas)
        {
            UtilReportOMS.UCTK uc = new UtilReportOMS.UCTK(rpttam.ToString(), rpttam, dvqly, FieldsNames, Formulas);
            uc.Exit += new OnExit(uc_Exit); 
            uc.Disposed += new EventHandler(uc_Disposed);
            Host1.Child = uc;
            uc.UserControl1_Load();
        }

        void uc_Disposed(object sender, EventArgs e)
        {
            if (_status == STATUS.ON_POPUP)
            {
                ParentPopup.IsOpen = false;
            }
            else
            {
                ParentWindow.Close();
            }
        }

        public void setinfor(CrystalDecisions.CrystalReports.Engine.ReportDocument rpttam, string dvqly)
        {
            UtilReportOMS.UCTK uc = new UtilReportOMS.UCTK(rpttam.ToString(), rpttam, dvqly);
            uc.Exit +=new OnExit(uc_Exit); 
            uc.Disposed += new EventHandler(uc_Disposed);
            Host1.Child = uc;
            uc.UserControl1_Load();
        }

#endregion
      
        //Exit popup
        void uc_Exit(object sender)
        {
            uc_Disposed(this, new EventArgs());
        }
       
    }
}
